require(["config"], function() {
    require(["jquery","header", "cookie"], function($) {
        function Confirm() {
            this.verify();
            this.loadAddress();
        }
        $.extend(Confirm.prototype, {
            verify: function() {
                $.cookie.json = true;
                var user = $.cookie("login-user");
                if (!user) // 用户未登录
                    location = "/html/login.html";
            },
            // 加载地址数据
            loadAddress: function() {
                this.loadProvince();
                $(".province").on("change", this.loadCity);
                $(".city").on("change", this.loadDistrict);
            },
            // 加载省份
            loadProvince: function() {
                var url1 = "http://route.showapi.com/1149-1?showapi_appid=29550&showapi_sign=fe01daee5a6a400c91e9f8523813fc61&level=1&page=1&maxSize=20",
                      url2 = "http://route.showapi.com/1149-1?showapi_appid=29550&showapi_sign=fe01daee5a6a400c91e9f8523813fc61&level=1&page=2&maxSize=20";
                $.when($.ajax(url1), $.ajax(url2))
                  .done(function(data1, data2) {
                    console.log(arguments)
                    var html = '<option value="-1">请选择省份</option>';
                    data1[0].showapi_res_body.data
                        .concat(data2[0].showapi_res_body.data)
                        .forEach(function(curr) {
                            html += `<option value="${curr.id}">${curr.areaName}</option>`;
                        });
                    $(".province").html(html);
                  });
            },
            // 加载城市
            loadCity: function() {
                var id = $(".province").val();
                var url = `http://route.showapi.com/1149-2?showapi_appid=29550&showapi_sign=fe01daee5a6a400c91e9f8523813fc61&parentId=${id}`;
                $.ajax(url).done(function(data) {
                    var html = '<option value="-1">请选择城市</option>';
                    data.showapi_res_body.data.forEach(function(curr) {
                        html += `<option value="${curr.id}">${curr.areaName}</option>`;
                    });
                    $(".city").html(html);
                });
            },
            // 加载区县
            loadDistrict: function() {
                var id = $(".city").val();
                var url = `http://route.showapi.com/1149-2?showapi_appid=29550&showapi_sign=fe01daee5a6a400c91e9f8523813fc61&parentId=${id}`;
                $.ajax(url).done(function(data) {
                    var html = '<option value="-1">请选择区县</option>';
                    data.showapi_res_body.data.forEach(function(curr) {
                        html += `<option value="${curr.id}">${curr.areaName}</option>`;
                    });
                    $(".district").html(html);
                });
            }
        });

        new Confirm();
    });
});